import { APP_CONFIG,AUTH_KEY } from "@/common/config.js";
import { Cache } from "@/utils/util";
import * as util from "@/utils/util";
import * as config from "@/common/config.js";
import { getCurrentPageUrlWithArgs } from "@/utils/util";

/**
 *注入全局方法
 */
let navigateFlag = false;
export const $authRoute = (o, auth = 1) => {
	
  if (navigateFlag) return false;
  // debugger
  console.log('到这里了');
  navigateFlag = true;
  o.animationType = o.animationType ? o.animationType : "pop-in";
  if (auth && !Cache(AUTH_KEY)) {
    if (getCurrentPageUrlWithArgs().indexOf("login") !== -1) return;
    Cache("back_url", getCurrentPageUrlWithArgs());
    uni.$u.toast("您还未登录，请先登录！");
	navigateFlag = false;
    return uni.$u.route({
      url: config.APP_CONFIG.LOGIN,
      type: "reLaunch",
      animationType: o.animationType,
      params: o.params ? o.params : {},
    });
    
  }
  if (o.url.indexOf("tabbar") !== -1) {
    o.type = "switchTab";
  }
  console.log("跳转。。。。", o,navigateFlag);
  uni.$u.route(o);
  setTimeout(() => {
    navigateFlag = false;
  }, 200);
};

const install = (Vue, vm) => {
  Vue.prototype.$getUrlParams = (param, k, p) => {
    if (typeof param != "string") return {};
    k = k ? k : "&"; //整体参数分隔符
    p = p ? p : "="; //单个参数分隔符
    var value = {};
    if (param.indexOf(k) !== -1) {
      param = param.split(k);
      for (var val in param) {
        if (param[val].indexOf(p) !== -1) {
          var item = param[val].split(p);
          value[item[0]] = item[1];
        }
      }
    } else if (param.indexOf(p) !== -1) {
      var item = param.split(p);
      value[item[0]] = item[1];
    } else {
      return param;
    }
    return value;
  };
  //全局工具方法
  Vue.prototype.$util = util;
  //全局配置
  Vue.prototype.$config = config;

  Vue.prototype.$authRoute = $authRoute;
  // 获取当前页面的url地址
  Vue.prototype.$getUrl = () => {
    var pages = getCurrentPages(); //获取加载的页面
    var currentPage = pages[pages.length - 1]; //获取当前页面的对象
    var url = "/" + currentPage.route; //当前页面url
    var options = currentPage.options; //如果要获取url中所带的参数可以查看options
    //拼接url的参数
    var urlWithArgs = url + "?";
    for (var key in options) {
      var value = options[key];
      urlWithArgs += key + "=" + value + "&";
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1);

    return urlWithArgs;
  };
  // 判断必要参数
  Vue.prototype.$p = (p, arr) => {
    return arr.every((item) => item in p);
  };
  // 判断登录状态
  Vue.prototype.$auth = (cb) => {
    // 如果未登录
    if (!Cache(AUTH_KEY)) {
      Cache("back_url", getCurrentPageUrlWithArgs());
      Vue.prototype.$t("您还未登录，请先登录！", () => {
        uni.$u.route(APP_CONFIG.LOGIN, {
          type: "redirectTo",
        });
      });
      return false;
    } else {
      cb && cb();
      return true;
    }
  };
  // 是否是微信H5
  Vue.prototype.$iswx = () => {
    // #ifdef H5
    var ua = window.navigator.userAgent.toLowerCase();
    if (
      ua.match(/MicroMessenger/i) == "micromessenger" ||
      ua.match(/QQ/i) == "qq"
    ) {
      return true;
    } else {
      return false;
    }
    // #endif
    // #ifndef H5
    return false;
    // #endif
  };

  // 判断是否登录
  Vue.prototype.$isLogin = function () {
    return Cache(AUTH_KEY);
  };
  Vue.prototype.$toast = function (title) {
    return uni.$u.toast(title);
  };
  Vue.prototype.$loading = function (msg = "", success, fail) {
    return uni.showLoading({
      title: msg,
      success: success,
      fail: fail,
    });
  };
  Vue.prototype.hideLoading = function () {
    return uni.hideLoading();
  };
  Vue.prototype.$copy = function (msg) {
    return new Promise((resolve, reject) => {
      uni.setClipboardData({
        data: msg,
        success() {
          resolve();
        },
        fail(error) {
          reject(error);
        },
      });
    });
  };
};

export default {
  install,
  $authRoute,
};
